package cn.ecasoft.service;

import cn.ecasoft.pojo.ClientSocket;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: LiangChun
 * @Description: socket池
 * @DateTime: 2023/2/23
 **/
public class SocketPool {
    private static final ConcurrentHashMap<String, ClientSocket> ONLINE_SOCKET_MAP = new ConcurrentHashMap<>();


    public static void add(ClientSocket clientSocket){
        if (clientSocket != null && !clientSocket.getKey().isEmpty())
            ONLINE_SOCKET_MAP.put(clientSocket.getKey(), clientSocket);
    }

    public static void remove(String key){
        if (!key.isEmpty())
            ONLINE_SOCKET_MAP.remove(key);
    }

    public static ClientSocket get(String key){
        if (!key.isEmpty()) {
            return ONLINE_SOCKET_MAP.get(key) ==null ?new ClientSocket():ONLINE_SOCKET_MAP.get(key);
        }else{
            return new ClientSocket();
        }
    }

    public static Boolean exist(String key) {
        return ONLINE_SOCKET_MAP.get(key) == null ? false : true;
    }
}
